import { defineConfig, CommonServerOptions } from 'vite'
import vue from '@vitejs/plugin-vue'
import dotenv, { DotenvParseOutput } from 'dotenv'
import fs from 'fs'
import path from 'path'

// https://vitejs.dev/config/
export default defineConfig((mode) => {
  const envFileName: string = '.env'
  const curFileName = `${envFileName}.${mode.mode}`
  let server: CommonServerOptions = {}
  const envData = fs.readFileSync(curFileName)
  const envMap: DotenvParseOutput = dotenv.parse(envData)

  if (mode.mode === 'development') {
    server = {
      host: envMap.VITE_HOST,
      port: Number(envMap.VITE_PORT),
      proxy: {

      }
    }
  } else if (mode.mode === 'production') {
    server = {
      host: envMap.VITE_HOST,
      port: Number(envMap.VITE_PORT)
    }
  }

  return {
    plugins: [vue()],
    baseUrl: '/',
    resolve: {
      alias: {
         '@': path.resolve(__dirname, 'src')
      }
    },
    server
  }
})
